package com.nbst.service.base.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.nbst.comnutil.LogOut;
import com.nbst.comnutil.NormalResult;
import com.nbst.comnutil.StringUtil;
import com.nbst.comnutil.UpdateShiroPermi;
import com.nbst.dao.mapper.mes.RolePermissionMapper;
import com.nbst.model.base.RolePermission;
import com.nbst.service.base.IRolePermissionService;

/**
 * @ClassName: RolePermissionServiceImpl
 * @Description:角色与权限的关系Service
 * @author 兵
 * @date 2017-8-17
 * 
 */
@Service
public class RolePermissionServiceImpl implements IRolePermissionService {

	@Autowired
	LogOut logout;

	@Autowired
	RolePermissionMapper rolepermissionmapper;

	@Override
	public NormalResult deleteByPrimaryKey(String prId) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			num = rolepermissionmapper.deleteByPrimaryKey(prId);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("删除成功");
				// 清空权限缓存
				UpdateShiroPermi.cleanShiro();
			}
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("删除失败");
			result.setDataset(e);
		}
		return result;
	}

	@Override
	public NormalResult insertSelective(Map<String,Object> map) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			num = rolepermissionmapper.insertSelective(map);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("新增成功");
				// 清空权限缓存
				UpdateShiroPermi.cleanShiro();
			}
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("新增失败");
			result.setDataset(e);
		}
		return result;
	}

	@Override
	public List<RolePermission> findByCondition(Map<String,Object> map) {
		List<RolePermission> list = new ArrayList<RolePermission>();
		try {
			list = rolepermissionmapper.findByCondition(map);
		} catch (Exception e) {
			logout.error(e);
		}

		return list;
	}

	@Override
	public NormalResult updateByPrimaryKeySelective(RolePermission record) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			if (StringUtil.isEmpty(record.getpId())) {
				result.setCode("9999");
				result.setMessage("权限id为空");
				return result;
			}
			if (StringUtil.isEmpty(record.getrId())) {
				result.setCode("9999");
				result.setMessage("角色id为空");
				return result;
			}
			num = rolepermissionmapper.updateByPrimaryKeySelective(record);
			/*
			 * if (num == 1) { result.setCode("0000"); if
			 * (record.getfClosed().intValue()==0) { result.setMessage("删除成功"); } else {
			 * result.setMessage("修改成功"); // 清空权限缓存 UpdateShiroPermi.cleanShiro(); } } else
			 * { result.setCode("9999"); }
			 */
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("出错了,刷新后再试试吧");
			result.setDataset(e);
		}
		return result;
	}

	@Override
	@Transactional // 开启事物
	public NormalResult roleBulkaddPermission(Integer rId, String[] pIds) {
		NormalResult result = new NormalResult();
		Map<String,Object> map = new HashMap<>();
		map.put("rId", rId);
		int i = 0;
		try {

			if (pIds.length > 0) {
				for (i = 0; i < pIds.length; i++) {
					map.put("rId", rId);
					map.put("pId", pIds[i]);
					rolepermissionmapper.insertSelective(map);
				}
				if (i == pIds.length) {
					result.setCode("0000");
					result.setMessage("新增成功");
				} else {
					result.setCode("9999");
					result.setMessage("新增失败");
				}
			} else {
				result.setCode("0000");
				result.setMessage("新增成功");
				// 清空权限缓存
				UpdateShiroPermi.cleanShiro();
			}
		} catch (Exception e) {
			logout.error(e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();// 手动回滚
		}
		return result;
	}

	@Override
	public NormalResult deleteByrId(Integer rId) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			num = rolepermissionmapper.deleteByrId(rId);
			if (num > 0) {
				result.setCode("0000");
				result.setMessage("删除成功");
				// 清空权限缓存
				UpdateShiroPermi.cleanShiro();
			}
			result.setCode("0000");
			result.setMessage("操作成功");
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("删除失败");
			result.setDataset(e);
		}
		return result;
	}

}